/**
 * Copyright (C) 2011 Taobao Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.taobao.jaket.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Helper class to process {@link Type} related functions
 * 
 * @author Xueqiang Mi
 */
public final class TypeUtil {

    public static final List<Method> getMethods(final Class<?> type) {
        List<Method> methods = new ArrayList<Method>();
        Set<Class<?>> classes = getInterfaces(type);
        if (classes != null) {
            for (Class<?> clazz : classes) {
                Method[] tmp = clazz.getDeclaredMethods();
                methods.addAll(Arrays.asList(tmp));
            }
        }
        return methods;
    }

    /**
     * @return never null
     */
    public static final Set<Class<?>> getInterfaces(final Class<?> type) {
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        interfaces.add(type);

        Class<?>[] classes = type.getInterfaces();
        if (classes != null) {
            for (Class<?> clazz : classes) {
                Set<Class<?>> tmp = getInterfaces(clazz);
                interfaces.addAll(tmp);
            }
        }
        return interfaces;
    }
}
